package cn.yejinwen.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSON;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import cn.chenjifeng.imple.OrderImple;
import cn.chenjifeng.model.OrderModel;
import cn.common.tool.DealFontToBackArray;
import cn.common.tool.Head;
import cn.common.tool.JsonObject;
import cn.common.tool.dele;
import cn.common.tool.seach;
import cn.mofuxiong.Model.login_register;
import cn.xiezonjie.model.Menu;
import cn.yejinwen.impl.OrderCartImpl;
import cn.yejinwen.model.Admin_id;
import cn.yejinwen.model.Desk_id;
import cn.yejinwen.model.Menu_id;
import cn.yejinwen.model.Order;
import cn.yejinwen.model.PayModel;
import cn.yejinwen.model.SekectVid;

import com.opensymphony.xwork2.ActionSupport;

public class OrderCartAction extends ActionSupport{
	private static final long serialVersionUID = 1L;
	HttpServletResponse response =  ServletActionContext.getResponse();
	HttpServletRequest req = ServletActionContext.getRequest();
	Menu_id m_id =new Menu_id();   //菜品表id
	Admin_id a_id=new Admin_id();  //管理员表id
	Desk_id t_id =new Desk_id();   //餐桌表id
	ArrayList<Menu> list;
	ArrayList<Order> list1;
	
	
	/*用于存储餐桌 菜品 菜品数量 */
	private static PayModel pm;	
	
	/*
	 * 插入订餐车
	 * 首次点菜会有一个新的t_id在orders中
	 */
	public String addCart()  throws SQLException, IOException{
		try
		{
			OrderCartImpl o_catr=new OrderCartImpl();		
			String a=(String)req.getSession().getAttribute("admin");//获取用户id	
			String str[]=req.getParameter("p").split(",");//页面传过来的菜品id 和餐桌id
			m_id.setM_id(str[0]);
			a_id.setA_id(a);
			t_id.setT_id(str[1]);
			
			System.out.println("添加到order表时的  用户名  餐桌id 菜品id  为 ：  "+a_id.getA_id()+" "+t_id.getT_id()+" "+m_id.getM_id());
			
			response.reset();
	 		response.setCharacterEncoding("UTF-8");
	 		response.setContentType("text/html; charset=utf-8");

	 		PrintWriter pw = response.getWriter();	
			
			int s=o_catr.AddCart(m_id,t_id,a_id);
			
					
	 		if(s==1)
	 		{
				pw.write(JSONArray.fromObject(new JsonObject(new Head(200,"添加到order表成功",false),null)).toString());
			}
	 		else
	 		{
				pw.write(JSONArray.fromObject(new JsonObject(new Head(500,"添加到order表失败",false),null)).toString());
			}
		
		}
		catch(Exception e)
		{
			System.out.println(e);
			PrintWriter pw = response.getWriter();
			pw.write(JSONArray.fromObject(new JsonObject(new Head(500,"添加到order表失败",false),null)).toString());
		}
		
		return null;
	}
    	
	
	
	public String JieSuan()
	{
		pm=new PayModel();
		
		String m_id_s = ServletActionContext.getRequest().getParameter("m_id");
		String id_number_s = ServletActionContext.getRequest().getParameter("id_number");
		String zonjiaa_s = ServletActionContext.getRequest().getParameter("zonjiaa");
		String t_id =  ServletActionContext.getRequest().getParameter("t_id");
		
		DealFontToBackArray dlta = new DealFontToBackArray();
		char m_id[] = dlta.DealArray(m_id_s);
		char id_number[] = dlta.DealArray(id_number_s);
				
		pm.setM_id(m_id);
		pm.setM_number(id_number);
		pm.setT_id(Integer.parseInt(t_id)); 
		pm.setM_summ(Integer.parseInt(zonjiaa_s));
		pm.setA_id((String) ServletActionContext.getRequest().getSession().getAttribute("UserId_id"));
		//pm.setV_id("1");  /*v_id得在另一个页面进行获取*/
		
		
		return null;
	}
	
	
	
	public String pay() throws SQLException
	{
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
		String time_data = df.format(new Date())+"";
		
		String v_id = ServletActionContext.getRequest().getParameter("v_id");
		dele de = new dele();
		DealFontToBackArray da = new DealFontToBackArray();
		
		String sql="update desk set t_status='1' where t_id="+pm.getT_id();
		de.dele(sql);
		
		sql="delete from orders where t_id="+pm.getT_id();
		de.dele(sql);
		
		pm.setV_id(ServletActionContext.getRequest().getParameter("select_vid"));
		System.out.println("所选的vipid : "+pm.getV_id());
		
		
		char temp_mid[] = pm.getM_id();		
		char temp_mnumber[] = pm.getM_number();
		int j=da.CharReallength(temp_mnumber);
		for(int i=0;i<j;i++)
		{
			
			//状态的字段预留在此，修改时直接修改sql语句即可
			sql ="insert into payed (t_id,m_id,a_id,v_id,m_sum,m_number,pm_time) values ("
				 + pm.getT_id()	 +","
				 +pm.getM_id()[i]+","
				 +pm.getA_id()	+","
				 +pm.getV_id()	+","
				 +pm.getM_summ()+","
				 +pm.getM_number()[i]+","
				 +"'"+time_data+"'"+
				 ")";
			de.dele(sql);
		}
		
		return "payed";
	}
	
		
	
	/*查询用户账号，用于结算时的选择*/
	public String select_vip_id() throws SQLException, IOException
	{
		
		ArrayList<SekectVid> al = new ArrayList<SekectVid>();
		String sql="select v_name,v_id from vip";
		seach s =  new seach();
		ResultSet rs = s.f(sql);
		
		
		while(rs!=null&&rs.next())
		{
			SekectVid sv =new SekectVid();
			sv.setV_id(rs.getString("v_id"));
			sv.setV_name(rs.getString("v_name"));
			al.add(sv);			
		}
		
		
		
		response.reset();
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html; charset=utf-8");
		PrintWriter pw = response.getWriter();
		pw.write(JSONArray.fromObject(al).toString());
		
		
		return null;
	}
	/*查询用户账号，用于结算时的选择*/
	
	
	
	
	public Menu_id getM_id() {
		return m_id;
	}


	public Admin_id getA_id() {
		return a_id;
	}


	public Desk_id getT_id() {
		return t_id;
	}
}
